package configModify;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.ini4j.Ini;

public class IniOprater {

	public static void main(String[] args) {
        try {
            System.out.println(readIni("DeviceOfTest", "TypeList", "NULL"));
            writeIni("java", "spring", "ioc,aop,core...");
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
    private static String readIni(String sectionArg, String key, String defaultValue) throws Exception{
        Ini ini = new Ini(new File("src/main/resources/iniFile/ebAgent.ini"));
        System.out.println("读取配置文件[section] = " + sectionArg + ", key = "+ key + " 的值为:" );
//        Ini.Section section = ini.get(sectionArg);
//        String url = section.get(key);
//        System.out.println(url);

        // or just use java.util.Map interface
        Map<String, String> map = ini.get(sectionArg);
//        double num = ini.get(sectionArg, key, Double.class);
        String value = map.get(key);

        if(null == value) {
            return  defaultValue;
        }
        
        return  value;
    }

    public static  void writeIni(String section, String key, String value) throws IOException {
        Ini ini = new Ini(new File("src/main/resources/iniFile/ebAgent.ini"));
        System.out.println("正在写入 [section]=" + section + ", key=" + key + ", value=" + value + " 的ini配置：");

        ini.put(section, key, value);

        ini.store();    // store iniConfig
        System.out.println("Write ini config is successful.");
    }
}
